﻿using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXDBC
    {
        public void SaveSDDAT(DataRow dwData)
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;

            setResult = QuerySDDAT(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapWAFARBEN(ref dwLocal, dwData);
            if (!flgExist)
            {
                InsertSDDAT(dwLocal);
            }
            UpdateSDDAT(dwLocal);
        }
        public void InsertSDDAT(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "INSERT INTO SDDAT(SDSA,SDKEY,SDDATEN)"
                + "VALUES (:SDSA,:SDKEY, :SDDATEN)";
            or1.Parameters.Add("SDSA", OracleType.NVarChar).Value = dwData["SDSA"];
            or1.Parameters.Add("SDKEY", OracleType.NVarChar).Value = dwData["SDKEY"];
            or1.Parameters.Add("SDDATEN", OracleType.NVarChar).Value = dwData["SDDATEN"];
            Connect2Proxy(or1);
        }
        public void DeleteSDDAT(string sdkey, string key)
        {

            OracleCommand or1 = OracleDB.CreateCommand();

            string SDKEY = "00" + Left(key + "                       ", 21) + "V";
            or1.CommandText = "SELECT * "
            + " FROM SDDAT WHERE SDSA=:SDSA AND SDKEY =:SDKEY";
            or1.Parameters.Add("SDSA", OracleType.NVarChar).Value = sdkey;
            or1.Parameters.Add("SDKEY", OracleType.NVarChar).Value = SDKEY;
            Connect2Proxy(or1);
        }
        public DataSet QuerySDDAT(DataRow dwSDDAT, int option = 1)
        {
            DataSet set4Return = new DataSet();
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            DataTable tbLocal = null;
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM SDDAT M WHERE ROWNUM<2";
                    break;
                case 1:
                    or1.CommandText = "SELECT * FROM SDDAT WHERE SDSA=:SDSA AND SDKEY =:SDKEY";
                    or1.Parameters.Add("SDSA", OracleType.NVarChar).Value = dwSDDAT["SDSA"].ToString(); ;
                    or1.Parameters.Add("SDKEY", OracleType.NVarChar).Value = dwSDDAT["SDKEY"].ToString();
                    break;
                case 2:
                    or1.CommandText = "SELECT * FROM SDDAT WHERE SDSA=:SDSA AND SDKEY =:SDKEY";
                    or1.Parameters.Add("SDSA", OracleType.NVarChar).Value = dwSDDAT["SDSA"].ToString();
                    or1.Parameters.Add("SDKEY", OracleType.NVarChar).Value = dwSDDAT["SDKEY"].ToString();
                    break;
                case 3:
                    or1.CommandText = "SELECT SDSA,SUBSTR(SDKEY,3,3)AS SDKEY,SUBSTR(SDDATEN,2,10) AS SDDATEN  FROM SDDAT WHERE SDSA=:SDSA"
                        + " AND SDKEY LIKE '%V'";
                    or1.Parameters.Add("SDSA", OracleType.NVarChar).Value = dwSDDAT["SDSA"].ToString();
                    break;
                case 9999:
                    or1.CommandText = "SELECT * FROM SDDAT WHERE SDSA=:SDSA";
                    or1.Parameters.Add("SDSA", OracleType.NVarChar).Value = dwSDDAT["SDSA"].ToString();
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
            {
                tbLocal = Result.Tables[0];
                tbLocal.TableName = "SDDAT";
                set4Return.Merge(Result);
            }
            return set4Return;
        }
        public void UpdateSDDAT(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "UPDATE SDDAT SET SDDATEN=:SDDATEN WHERE SDSA=:SDSA AND SDKEY=:SDKEY";
            or1.Parameters.Add("SDDATEN", OracleType.NVarChar).Value = dwData["SDDATEN"].ToString();
            or1.Parameters.Add("SDSA", OracleType.NVarChar).Value = dwData["SDSA"].ToString();
            or1.Parameters.Add("SDKEY", OracleType.NVarChar).Value = dwData["SDKEY"].ToString();
            Connect2Proxy(or1);
        }
    }
}
